/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.definitions;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 *
 * @author abracadabuda
 */
public class ViewDefinition extends AbstractDefinition {
    private String catalog;
    private String schema;
    private String name;
    private String viewName;
    private String comment;
    private ArrayList<ColumnDefinition> columns = new ArrayList<ColumnDefinition>();
    private String source;
    private DatabaseDefinition database;
    
    public ViewDefinition(DatabaseDefinition database)  {
        this.database = database;
        schema = AbstractDefinition.prefix;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public ArrayList<ColumnDefinition> getColumns() {
        return columns;
    }

    public void setColumns(ArrayList<ColumnDefinition> columns) {
        this.columns = columns;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getViewName() {
        return viewName;
    }

    public void setViewName(String viewName) {
        this.viewName = viewName;
    }
    
    /**
     * returns SQL code for view defintion (CREATE OR REPLACE VIEW ..)
     * @return SQL code
     */
    public String getSQL() {
        String sql = new String();
        sql += "CREATE OR REPLACE VIEW ";
        sql += "\"" + schema + "\".";
        sql += "\"" + viewName + "\" AS \n";
        sql += processSource();
        return sql.replaceAll("%%%", "\"" + prefix + "\"") + ";\n";
    }
    
    /**
     * view is stored in xml as a one big select, not divided by any columns or tables, etc.
     * Therefore this select must be parsed and schema names must be inserted
     * @return SQL code of internal view definition
     */
    private String processSource() {
        String beginStr = source.substring(0, source.indexOf(" FROM ") + " FROM ".length());
        String endstr  = source.indexOf(" WHERE ") == -1 ? "" : source.substring(source.indexOf(" WHERE "), source.length());
        String tablesStr = source.substring(source.indexOf(" FROM ") + " FROM ".length(), source.indexOf(" WHERE ") == -1 ? source.length() : source.indexOf(" WHERE "));
        StringTokenizer tables = new StringTokenizer(tablesStr, ",");
        tablesStr = "";
        while(tables.hasMoreTokens()) {
            String table = tables.nextToken().trim();
            tablesStr += "\"" + schema + "\"." + table;
            if(tables.hasMoreTokens()) 
                tablesStr += ",\n";
        }
        
        return beginStr + tablesStr + endstr;
    }
    
}
